package com.xiaotu.common.aop;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.JobLogModel;
import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.Constants.DataRedisKey;

/**
 * @类名 LogAspect
 * @日期 2017年2月21日
 * @作者 高海军
 * @功能 日志切面基类
 */
public abstract class ALogAspect
{
    @Autowired
    protected CacheHandler cacheHandler;

    private static final Logger LOGGER = LoggerFactory
            .getLogger(ALogAspect.class);

    public static void setLogInfo(Map<String, Object> targetMap,
            JobLogModel logModel)
    {
        setLogInfo(targetMap, logModel, "tvid", "starid");
    }

    public static void setLogInfo(Map<String, Object> targetMap,
            JobLogModel logModel, String tvId, String starId)
    {
        String targetType = getTargetType(targetMap, tvId, starId);

        Object name = null;
        targetType = targetType == null ? Constants.TargetType.OTHER : targetType;
        switch (targetType)
        {
            case Constants.TargetType.TV:
                logModel.setTargetId(targetMap.get(tvId).toString());
                name = targetMap.get("tvName");
                logModel.setTargetName(name == null ? null : name.toString());
                logModel.setTargetType(Constants.TargetType.TV);
                break;
            case Constants.TargetType.STAR:
                logModel.setTargetId(targetMap.get(starId).toString());
                name = targetMap.get("name");
                logModel.setTargetName(name == null ? null : name.toString());
                logModel.setTargetType(Constants.TargetType.STAR);
                break;
            case Constants.TargetType.OTHER:
                logModel.setTargetId(Constants.TargetType.OTHER_TYPE_ID);
                logModel.setTargetType(Constants.TargetType.OTHER);
                break;
            default:
                throw new SpiderException(SpiderExceptionCode.TargetTypeErrorException);
        }
    }

    public static String getTargetType(Map<String, Object> targetMap,
            String tvId, String starId)
    {
        if (targetMap == null || targetMap.isEmpty())
            throw new SpiderException(
                    SpiderExceptionCode.TargetInfoEmptyException);
        if (targetMap.get(tvId) != null)
            return Constants.TargetType.TV;
        else if (targetMap.get(starId) != null)
            return Constants.TargetType.STAR;
        else if (targetMap.get(Constants.TargetType.OTHER_TYPE_KEY) != null)
            return Constants.TargetType.OTHER;
        return null;
    }

    protected void saveLog(JobLogModel logModel)
    {
        try
        {
            String key = DataRedisKeyUtils
                    .getKey(DataRedisKey.PageProcess_Log_YYYYMMDD);
            cacheHandler.addListCache(key, logModel);
            cacheHandler.setCacheTimeout(key);
        }
        catch (Exception e)
        {
            LOGGER.error(StringUtils.EMPTY, e);
        }
    }
}
